package com.xym.manage.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.xym.common.bean.EasyUiResult;
import com.xym.manage.pojo.Item;
import com.xym.manage.pojo.ItemDesc;
import com.xym.manage.service.ItemDescService;
import com.xym.manage.service.ItemService;

@Controller
@RequestMapping(value = "/item")
public class ItemController {

	@Autowired
	private ItemService itemService;

	@Autowired
	private ItemDescService itemDescService;

	
	/**
	 * 逻辑删除  
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.PUT)
	public ResponseEntity<Void> delete(@RequestParam("ids") Long[] ids) {

		Boolean flag = itemService.deleteSetStatus(ids);
		if (flag) {
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	
	
	/**
	 * http://manage.xym.com/rest/item/instock 
	 * 异步请求，下架商品 参数 ids 1,2 返回值 状态码 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "instock", method = RequestMethod.PUT)
	public ResponseEntity<Void> instock(@RequestParam("ids") Long[] ids) {

		Boolean flag = itemService.instock(ids);
		if (flag) {
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	
	/**
	 * rest/item/reshelf  
	 * 异步请求，上架商品 参数 ids  返回值 状态码 200
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "reshelf", method = RequestMethod.PUT)
	public ResponseEntity<Void> reshelf(@RequestParam("ids") Long[] ids) {

		Boolean flag = itemService.reshelf(ids);
		if (flag) {
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	
	/**
	 * http://manage.xym.com/rest/item 修改 put请求 返回参数 状态码
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> uapadeItemAndDesc(Item item,
			@RequestParam("desc") String desc) {
		try {
			if (item.getId() == null || desc == null) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			Boolean flag = itemService.updata(item, desc);

			if (flag) {
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	/**
	 * 异步请求 '/rest/item/desc/'+data.id 编辑回显商品描述 返回data.itemDesc属性
	 */
	@RequestMapping(value = "desc/{id}", method = RequestMethod.GET)
	public ResponseEntity<ItemDesc> queryItemDescById(
			@PathVariable(value = "id") Long itemId) {
		try {
			ItemDesc itemDesc = itemDescService.queryById(itemId);
			if (itemDesc == null) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			return ResponseEntity.ok(itemDesc);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
				null);
	}

	/**
	 * 新增商品 参数 item对象 desc 描叙 返回 状态码
	 * 
	 * @param item
	 * @param desc
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> saveItem(Item item,
			@RequestParam(value = "desc") String desc) {
		try {
			if (StringUtils.isEmpty(item.getTitle())) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			/*
			 * item.setStatus(1); item.setId(null); itemService.save(item);
			 * 
			 * ItemDesc itemDesc= new ItemDesc();
			 * itemDesc.setItemId(item.getId()); itemDesc.setItemDesc(desc);
			 * itemDescService.save(itemDesc);
			 */
			Boolean flag = itemService.saveItemAndItemDesc(item, desc);
			if (flag) {
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}

	/**
	 * http://www.xym.com/rest/item?page=1&rows=30&_=1514341080104 
	 * 商品list展示 参数
	 * page rows 返回easyui-datagrid 总条数total 分页数据rows
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<EasyUiResult> queryItemlist(
			@RequestParam("page") Integer pageNum,
			@RequestParam("rows") Integer rows) {
            EasyUiResult result = new EasyUiResult();
		try {
			List<Item> list = itemService.queryPageList(
					pageNum, rows);
			if (list == null || list.size() == 0) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			}
			result.setTotal((long)itemService.queryCountByStatus());
			result.setRows(list);
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
				null);
	}
}
